package com.pap.sequences.sequence.controller;

import com.pap.base.util.bean.BeanCopyUtilss;
import com.pap.beans.idworker.IdWorker;
import com.pap.obj.vo.logineduser.LoginedUser;
import com.pap.obj.vo.logineduser.LoginedUserVO;
import com.pap.obj.vo.page.Page;
import com.pap.obj.vo.response.ResponseVO;
import com.pap.sequences.dto.SysSequenceDTO;
import com.pap.sequences.sequence.entity.SysSequence;
import com.pap.sequences.sequence.service.ISysSequenceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/syssequence")
@Api(value = "号码对象接口", tags = "号码对象接口", description="号码对象接口")
public class SysSequenceController {

	private static Logger logger  =  LoggerFactory.getLogger(SysSequenceController.class);
	
	@Resource(name = "sysSequenceService")
	private ISysSequenceService sysSequenceService;

	@Resource(name = "idWorker")
	private IdWorker idWorker;

	@ApiOperation("创建")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "sysSequenceDTO", value = "", required = true, dataType = "SysSequenceDTO", paramType="body")
	})
	@PostMapping(value = "")
	public ResponseVO<SysSequenceDTO> create(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
											 @RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
											 @RequestBody SysSequenceDTO sysSequenceDTO,
											 @LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){
		String idStr = idWorker.nextIdStr();
		sysSequenceDTO.setSequenceId(idStr);

		SysSequence databaseObj = new SysSequence();
		BeanCopyUtilss.myCopyProperties(sysSequenceDTO, databaseObj, loginedUserVO);

		int i = sysSequenceService.insertSelective(databaseObj);

		return ResponseVO.successdata(sysSequenceDTO);
	}

	@ApiOperation("更新")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "sysSequenceDTO", value = "", required = true, dataType = "SysSequenceDTO", paramType="body")
	})
	@PutMapping(value = "")
	public ResponseVO<SysSequenceDTO> update(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@RequestBody SysSequenceDTO sysSequenceDTO,
													@LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){

		SysSequence databaseObj = new SysSequence();
		BeanCopyUtilss.myCopyProperties(sysSequenceDTO, databaseObj, loginedUserVO, false);
		int i = sysSequenceService.updateByPrimaryKeySelective(databaseObj);

		databaseObj = sysSequenceService.selectByPrimaryKey(databaseObj.getSequenceId());

		return ResponseVO.successdata(databaseObj);
	}


	@ApiOperation("删除")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "sysSequenceDTO", value = "", required = true, dataType = "SysSequenceDTO", paramType="body")
	})
	@DeleteMapping(value = "")
	public ResponseVO<String> delete(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@RequestBody SysSequenceDTO sysSequenceDTO,
													@LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){

		SysSequence databaseObj = new SysSequence();
		BeanCopyUtilss.myCopyProperties(sysSequenceDTO, databaseObj);

		int i = sysSequenceService.deleteByPrimaryKey(databaseObj.getSequenceId());

		return ResponseVO.successdata("操作成功");
	}

	@ApiOperation("详情")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "String", paramType="path")
	})
	@GetMapping(value = "/{id}")
	public ResponseVO<SysSequenceDTO> selectOne(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@PathVariable("id") String id){

		SysSequence databaseObj = sysSequenceService.selectByPrimaryKey(id);

		return ResponseVO.successdata(databaseObj);
	}

	@ApiOperation("查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "sysSequenceDTO", value = "应用查询参数", required = false, dataType = "SysSequenceDTO", paramType="body"),
		@ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int", paramType="query"),
		@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType = "int", paramType="query"),
		@ApiImplicitParam(name = "sortBy", value = "按照哪个属性排序", required = false, dataType = "String", paramType="query"),
		@ApiImplicitParam(name = "order", value = "排序顺序", required = false, dataType = "String", paramType="query")
	})
	@PostMapping(value = "/query")
	public ResponseVO<SysSequenceDTO>query(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
												@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
												@RequestBody SysSequenceDTO sysSequenceDTO,
												@RequestParam Integer pageNo, @RequestParam Integer pageSize,
												@RequestParam(defaultValue = "create_time") String sortBy, @RequestParam(defaultValue = "desc") String order,
												@LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){
		Map<Object, Object> map = BeanCopyUtilss.beanToMap(sysSequenceDTO, loginedUserVO);
		if(pageSize != 0) {
			Page newPage = new Page(pageNo + "", pageSize + "");
			map.put("page", newPage);
		}
		List<SysSequence> list = sysSequenceService.selectListByMap(map);

		//
		List<SysSequenceDTO> dtoList = toDTO(list);

		if(pageSize != 0) {
			Page newPage = new Page(pageNo + "", pageSize + "");
			int countNum = sysSequenceService.selectCountByMap(map);
			newPage.setCount(countNum);
			return ResponseVO.successdatas(dtoList, newPage);
		} else {
			return ResponseVO.successdatas(dtoList, null);
		}

	}

	private List<SysSequenceDTO> toDTO(List<SysSequence> databaseList) {
		List<SysSequenceDTO> returnList = new ArrayList<SysSequenceDTO>();
		if(databaseList != null) {
			for(SysSequence dbEntity : databaseList) {
				SysSequenceDTO dtoTemp = new SysSequenceDTO();
				BeanCopyUtilss.myCopyProperties(dbEntity, dtoTemp);
				returnList.add(dtoTemp);
			}
		}
		return returnList;
	}

	private List<SysSequence> toDB(List<SysSequenceDTO> dtoList) {
		List<SysSequence> returnList = new ArrayList<SysSequence>();
		if(dtoList != null) {
			for(SysSequenceDTO dtoEntity : dtoList) {
				SysSequence dbTemp = new SysSequence();
				BeanCopyUtilss.myCopyProperties(dtoEntity, dbTemp);
				returnList.add(dbTemp);
			}
		}
		return returnList;
	}
}
